package com.liu.user.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.github.pagehelper.PageInfo;
import com.liu.common.entity.Result;
import com.liu.common.entity.StatusCode;
import com.liu.common.service.GetTokenService;
import com.liu.user.entity.base.User;
import com.liu.user.entity.qo.UserLoginInfoQO;
import com.liu.user.entity.qo.UserModifyInfoQO;
import com.liu.user.entity.qo.UserRegisterInfoQO;
import com.liu.user.entity.vo.DeductionInfoVO;
import com.liu.user.entity.vo.UserInfoVO;
import com.liu.user.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;


@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private GetTokenService getTokenService;

    /**
     * 用户注册
     * @param userRegisterInfoQO 用户输入的注册信息
     * @return
     */
    @CrossOrigin(origins = "http://localhost:8080", maxAge = 3600)
    @PostMapping("/register")
    public Result register(@RequestBody UserRegisterInfoQO userRegisterInfoQO){
        User checkUsername = userService.registerCheckUsername(userRegisterInfoQO.getUsername());
        if (checkUsername != null){
            return new Result(false, StatusCode.ERROR,"用户名已经存在，注册失败");
        }else {
            Date date = new Date();
            userRegisterInfoQO.setCreateTime(date);
            userService.register(userRegisterInfoQO);
            return new Result(true, StatusCode.OK,"注册成功");
        }
    }

    /**
     * 用户登陆
     * @param userLoginInfoQO
     * @return
     */
    @CrossOrigin(origins = "http://localhost:8080", maxAge = 3600)
    @PostMapping("/login")
    public Result login(@RequestBody UserLoginInfoQO userLoginInfoQO){
        if (userLoginInfoQO.getUsername() != null){
            User loginUserInfo = userService.login(userLoginInfoQO.getUsername());
            if (loginUserInfo == null){
                return new Result(false,StatusCode.ERROR,"用户名不存在");
            }
            if (userLoginInfoQO.getPassword() != null && userLoginInfoQO.getPassword().equals(loginUserInfo.getPassword())){
                StpUtil.login(loginUserInfo.getId());
                String tokenValue = StpUtil.getTokenValue();
                User user = new User();
                user.setId(loginUserInfo.getId());
                user.setLastLoginTime(new Date());
                userService.recordUserInfo(user);
                return new Result(true,StatusCode.OK,"登陆成功",tokenValue);
            }else {
                return new Result(false,StatusCode.ERROR,"密码错误");
            }
        }else {
            return new Result(false,StatusCode.ERROR,"请输入用户名和密码");
        }
    }

    /**
     * 查询登陆的用户信息
     * @param
     * @return
     */
    @CrossOrigin(origins = "http://localhost:8080", maxAge = 3600)
    @GetMapping("/getUserInfo")
    public Result getUserInfo(){
        String token = getTokenService.getToken();
        Object loginIdByToken = StpUtil.getLoginIdByToken(token);
        if (loginIdByToken != null){
            UserInfoVO userInfo = userService.getUserInfo(Integer.valueOf(loginIdByToken.toString()));
            return new Result(true,StatusCode.OK,"已登录",userInfo);
        } else {
            return new Result(false,StatusCode.ERROR,"未登录");
        }
    }

    /**
     * 用户退出登陆
     * @return
     */
    @CrossOrigin(origins = "http://localhost:8080", maxAge = 3600)
    @GetMapping("/loginOut")
    public Result loginOut(){
        String token = getTokenService.getToken();
        Object loginIdByToken = StpUtil.getLoginIdByToken(token);
        StpUtil.logout(loginIdByToken);
        return new Result(true,StatusCode.OK,"退出登陆成功");
    }

    /**
     * 用户信息修改
     * @param userModifyInfoQO
     * @return
     */
    @CrossOrigin(origins = "http://localhost:8080", maxAge = 3600, allowedHeaders = "*")
    @PostMapping("/modifyUserInfo")
    public Result modifyUserInfo(@RequestBody UserModifyInfoQO userModifyInfoQO){
        String token = getTokenService.getToken();
        Object loginIdByToken = StpUtil.getLoginIdByToken(token);
        // 修改密码
        if (userModifyInfoQO.getModifyType() == 1){
            UserInfoVO userInfo = userService.getUserInfo(Integer.valueOf(loginIdByToken.toString()));
            if (userInfo.getPassword().equals(userModifyInfoQO.getOldPassword())){
                userService.modifyPassword(userModifyInfoQO.getNewPassword(),Integer.valueOf(loginIdByToken.toString()));
                return new Result(true,StatusCode.OK,"修改密码成功");
            } else {
                return new Result(false,StatusCode.ERROR,"修改密码失败，旧密码输入错误");
            }
        }
        // 修改用户信息
        if (userModifyInfoQO.getModifyType() == 2){
            User user = new User();
            user.setId(Integer.valueOf(loginIdByToken.toString()));
            user.setUsername(userModifyInfoQO.getUsername());
            user.setPhoneNumber(userModifyInfoQO.getPhoneNumber());
            user.setGender(userModifyInfoQO.getGender());
            userService.recordUserInfo(user);
            return new Result(true,StatusCode.OK,"修改用户信息成功");
        }
        return new Result(false,StatusCode.ERROR,"修改用户信息失败");
    }

    @CrossOrigin(origins = "http://localhost:8080", maxAge = 3600, allowedHeaders = "*")
    @GetMapping("/queryDeductionInfo")
    public Result queryDeductionInfo(@RequestParam("pageNum")String pageNum, @RequestParam("pageSize")String pageSize){
        String token = getTokenService.getToken();
        Object loginIdByToken = StpUtil.getLoginIdByToken(token);
        if (loginIdByToken != null){
            PageInfo<DeductionInfoVO> deductionInfoVOPageInfo = userService.queryDeductionInfo(String.valueOf(loginIdByToken), pageNum, pageSize);
            return new Result(true,StatusCode.OK,"查询成功",deductionInfoVOPageInfo);
        }else {
            return new Result(false,StatusCode.ERROR,"查询失败，您还未登录");
        }
    }
}
